import { _decorator, Component, Node, Sprite, Label, Color, v3, tween, UITransform, SpriteFrame } from 'cc';
const { ccclass, property } = _decorator;

import SSS_TimerManager from "../../../Script/Manager/SSS_TimerManager";
import { SpiderSolitaire_GameData } from './SpiderSolitaire_GameData';
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
import CommonFunction from 'db://assets/Script/Utils/CommonFunction';

@ccclass('SpiderSolitaire_Card')
export default class SpiderSolitaire_Card extends Component {
    @property(Node)
    normal: Node | null = null;
    @property(Sprite)
    icon: Sprite | null = null;
    @property(Label)
    Label_point: Label | null = null;
    col: number = 0;
    row: number = 0;
    length: number = 1;
    //    // color;
    //    // Label_color;
    id: number;
    isComplete: boolean = false;
    目标 = {
        id: 0,
        col: 0,
        row: 0,
    };
    //    // 只序列化需要的属性
    toJSON() {
        return {
            col: this.col,
            row: this.row,
            id: this.id,
            // _class: 'MyClass' // 添加类标识以便反序列化
        };
    }
    //    // 反序列化方法
    static fromJSON(data: any) {
        // return new SpiderSolitaire_Card(data.id, data.col, data.row);
    }
    //    // 初始化
    init(_col: number, _row: number, _id: number) {
        this.col = _col;
        this.row = _row;
        this.id = _id;

        // console.log(`x,y `, this.x, this.y);

        // let item_data = S2048_GameData.colors.find(e => e.id == this.id);
        // this.color = item_data.color;
        // this.Label_color = item_data.Label_color;

        this.node.active = true;
        this.InitWidthAndHeight(SpiderSolitaire_GameData.w, SpiderSolitaire_GameData.h);
        this.InitPosition();

        this.Refresh();
    }
    async Refresh() {
        this.RefreshIcon();

        // this.Refresh_number();
        // this.Refresh_color();
    }

    async RefreshIcon() {
        this.icon.node.active = true;
        bdlMgr.getRes<SpriteFrame>(`SpiderSolitaire_Sprite`, `Standard 52 Cards/Standard Cards/Spades/${this.id}_spade/spriteFrame`, SpriteFrame).then(res => {
            if (this.icon) {
                this.icon.spriteFrame = res;
                this.icon.sizeMode = Sprite.SizeMode.TRIMMED;
                // this.icon.node.scale = 1
                // CommonFunction.SetFrame(this.icon, 66, 66);
            }
        });
    }

    async Refresh_number() {
        this.Label_point.string = `${this.id}`;

        let item_data = SpiderSolitaire_GameData.colors.find(e => e.id == this.id);
        let Label_color = item_data.Label_color;
        this.Label_point.color = new Color().fromHEX(Label_color);
    }
    async Refresh_color() {
        // console.log(`color`, this.color);

        let item_data = SpiderSolitaire_GameData.colors.find(e => e.id == this.id);
        let color = item_data.color;
        this.icon.color = new Color().fromHEX(color);
        // console.log(`this.icon.node.color`, this.icon.node.color);
    }
    //    // 初始化单元宽高
    InitWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    InitPosition() {
        // let x = -SpiderSolitaire_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        // let y = -SpiderSolitaire_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * (this.row - 1);
        let x = this.Get_x();
        let y = this.Get_y();
        this.node.position = v3(x, y, 0);
    }

    Get_x() {
        console.log(`SpiderSolitaire_GameData.W`, SpiderSolitaire_GameData.W);
        console.log(`this.node.getComponent(UITransform).width`, this.node.getComponent(UITransform).width);
        
        let x = -SpiderSolitaire_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        return x;
    }
    Get_y() {
        let start_y = 0;
        let per_y = 36;
        let y = start_y - this.row * per_y;
        return y;
    }

    async MovePosition(_duration = 0.1) {
        let x = -SpiderSolitaire_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        let y = -SpiderSolitaire_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * (this.row - 1);
        let position = v3(x, y, 0);
        tween(this.node)
            .to(_duration, { position: position })
            .call(() => {
                // this.Refresh();
            })
            .start();

        await new Promise(res => SSS_TimerManager.Instance.scheduleOnce(res, _duration));
    }
    // Destroy() {
    //     this.node.destroy();
    // }
}